#!/usr/bin/env python
#
# Copyright (C) 2011 Clement Lorteau <clem@lorteau.fr>	
#		
# This file is part of QuickScreenShots.		
# 	
# This program is free software; you can redistribute it and/or modify	
# it under the terms of the GNU General Public License as published by		
# the Free Software Foundation; version 3.	
# 	
# This program is distributed in the hope that it will be useful,		
# but WITHOUT ANY WARRANTY; without even the implied warranty of		
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the		
# GNU General Public License for more details.		
# 		
# You should have received a copy of the GNU General Public License		
# along with this program; if not, write to the Free Software		
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.

import wx
import wx.lib.newevent
import platform
from __init__ import resources
from options import Options

ID_RESTORE = wx.NewId()
ID_SETTINGS = wx.ID_PREFERENCES
ID_DESKTOP = wx.NewId()
ID_WINDOW = wx.NewId()
ID_REGION = wx.NewId()
ID_QUIT = wx.ID_EXIT

SystrayEvent, EVT_SYSTRAY_EVENT = wx.lib.newevent.NewCommandEvent()

class QSSsystray(wx.TaskBarIcon):
    def __init__(self, opts, evthdl, *args, **kwds):
        self.evthdl = evthdl
        self.options = opts
        wx.TaskBarIcon.__init__(self, *args, **kwds)
        
        font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
        boldfont = wx.Font(font.GetPointSize(), font.GetFamily(), font.GetStyle(),
                           wx.FONTWEIGHT_BOLD, font.GetUnderlined(), font.GetFaceName(),
                           font.GetEncoding())
        
        self.settings_bmp = wx.Bitmap("%s/settings_16.png" % resources, wx.BITMAP_TYPE_PNG)
        self.desktop_bmp = wx.Bitmap("%s/desktop_20.png" % resources, wx.BITMAP_TYPE_PNG)
        self.window_bmp = wx.Bitmap("%s/window_20.png" % resources, wx.BITMAP_TYPE_PNG)
        self.region_bmp = wx.Bitmap("%s/region_20.png" % resources, wx.BITMAP_TYPE_PNG)
        
        self.menu = wx.Menu()
        self.restoremenu = wx.MenuItem(self.menu, ID_RESTORE, "&Restore", '', wx.ITEM_NORMAL)
        self.restoremenu.SetFont(boldfont)
        self.menu.AppendItem(self.restoremenu)
        self.menu.AppendSeparator()
        self.settingsmenu = wx.MenuItem(self.menu, ID_SETTINGS, "&Settings", '', wx.ITEM_NORMAL)
        self.settingsmenu.SetBitmap(wx.Bitmap("%s/settings_16.png" % resources, wx.BITMAP_TYPE_PNG))
        self.menu.AppendItem(self.settingsmenu)
        self.menu.AppendSeparator()
        self.desktopmenu = wx.MenuItem(self.menu, ID_DESKTOP,
                                       "Desktop screenshot\t%s" % self.options.kb_desktop.GetValue())
        self.desktopmenu.SetBitmap(wx.Bitmap("%s/desktop_20.png" % resources, wx.BITMAP_TYPE_PNG))
        self.menu.AppendItem(self.desktopmenu)
        self.windowmenu = wx.MenuItem(self.menu, ID_WINDOW,
                                      "Active window screenshot\t%s" % self.options.kb_window.GetValue())
        self.windowmenu.SetBitmap(wx.Bitmap("%s/window_20.png" % resources, wx.BITMAP_TYPE_PNG))
        self.menu.AppendItem(self.windowmenu)
        self.regionmenu = wx.MenuItem(self.menu, ID_REGION,
                                      "New region screenshot\t%s" % self.options.kb_region.GetValue())
        self.regionmenu.SetBitmap(wx.Bitmap("%s/region_20.png" % resources, wx.BITMAP_TYPE_PNG))
        self.menu.AppendItem(self.regionmenu)
        self.menu.AppendSeparator()
        self.quitmenu = wx.MenuItem(self.menu, ID_QUIT, 'Quit')
        self.quitmenu.SetBitmap(wx.Bitmap("%s/quit_16.png" % resources, wx.BITMAP_TYPE_PNG))
        self.menu.AppendItem(self.quitmenu)
        
        if platform.system() == 'Windows':
        	self.SetIcon(wx.Icon("%s/snapshot.ico" % resources, wx.BITMAP_TYPE_ICO), 'QuickScreenShots')
        elif platform.system() == 'Darwin':
        	self.SetIcon(wx.Icon("%s/snapshot.png" % resources, wx.BITMAP_TYPE_PNG), 'QuickScreenShots')
        	pass
        
        self.Bind(wx.EVT_TASKBAR_LEFT_DCLICK, self.OnLeftDClick)
        self.Bind(wx.EVT_MENU, self.OnMenu)
        
    def __restore(self):
        evt_obj = SystrayEvent(ID_RESTORE)
        self.evthdl.ProcessEvent(evt_obj)
        
    def OnMenu(self, event):
        evt_obj = SystrayEvent(event.GetId())
        self.evthdl.ProcessEvent(evt_obj)
    
    def OnLeftDClick(self, event):
        self.__restore()
        
    def CreatePopupMenu(self):
        self.PopupMenu(self.menu)

if __name__ == '__main__':
    def OnSystray(event):
        if event.GetId() == ID_QUIT:
            app.Exit()
        if event.GetId() == ID_RESTORE:
            print "Restore!"
        if event.GetId() == ID_SETTINGS:
            print "Settings!"
        if event.GetId() == ID_DESKTOP:
            print "Desktop screenshot!"
        if event.GetId() == ID_WINDOW:
            print "Window screenshot!"
        if event.GetId() == ID_REGION:
            print "Region screenshot!"
    opts = Options()
    app = wx.PySimpleApp(0)
    st = QSSsystray(opts, app)
    app.Bind(EVT_SYSTRAY_EVENT, OnSystray)
    app.MainLoop()